///Early life grade
clear
clear matrix
clear mata
cap log close
************************************************************************************
**change directory here

* DIRECTORY ROADMAP
sysdir set PLUS C:\ado\plus\c
************************************************************************************

global dir1 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Data
global dir2 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Do
global dir3 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Output

********************************************************************************

use "$dir1\Grade_final_early_life", clear

//////////////////////////////////////////////////////////////////////////////
///Main
///Table 3: baseline 
///panel A: Early life 

areg years_school shock_early_life1 gender genderhd i.agey i.year i.ROB [aw=weight], ab ( YOB ) cluster (ROB)
boottest shock_early_life1=0,  nograph  

outreg2 using "$dir3\cocoa_cognitive_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, No)
//////

areg years_school shock_early_life1 gender genderhd i.agey i.year i.ROB i.ROB#c.YOB [aw=weight], ab ( YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  

outreg2 using "$dir3\cocoa_cognitive_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////
////panel B: In-utero 
areg years_school shock_1 gender genderhd i.agey i.year i.ROB [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  

outreg2 using "$dir3\cocoa_cognitive_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, No)
///////////////////
areg years_school shock_1 gender genderhd i.agey i.year i.ROB i.ROB#c.YOB  [aw=weight], ab ( YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  

outreg2 using "$dir3\cocoa_cognitive_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
///Table 4: IV (international cocoa price)
//second stage

ivreg2 years_school (shock_early_life1=shock_earlyi1) gender genderhd i.agey i.year  i.ROB i.YOB [aw=weight], cluster (ROB)
boottest shock_early_life1=0 ,  nograph  

gen smpl_1=0
replace smpl_1=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

ivreg2 years_school (shock_early_life1=shock_earlyi1) gender genderhd i.agey i.year i.ROB#c.YOB i.ROB i.YOB [aw=weight], cluster (ROB)
boottest shock_early_life1=0 ,  nograph
  
gen smpl_2=0
replace smpl_2=1 if e(sample)==1
//outreg2 using "$dir3\second_stage_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

/////
ivreg2 years_school (shock_1=shock_1i) gender genderhd i.agey i.year  i.ROB i.YOB [aw=weight],  cluster (ROB)
boottest shock_1=0 ,  nograph 
  
gen smpl_3=0
replace smpl_3=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

ivreg2 years_school (shock_1=shock_1i) gender genderhd i.agey i.year i.ROB#c.YOB i.ROB i.YOB [aw=weight],  cluster (ROB)
boottest shock_1=0 ,  noci nograph 
 
gen smpl_4=0
replace smpl_4=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
////
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
///Table B.2 (Panel B)
global des1 "years_school agey gender genderhd"

keep if years_school!=.
eststo A: estpost summarize $des1 

esttab A using "$dir3\Summary_school.tex", replace  main(mean %6.2f sd ) aux(count)
esttab A using "$dir3\Summary_school.tex", replace label cell(" mean(fmt(3)) sd(fmt(3)) count(fmt(0))") 
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
///////Table C.2: IV (international cocoa price)
//first stage 

areg  shock_early_life1 shock_earlyi1 gender genderhd i.agey i.year  i.ROB [aw=weight] if smpl_1==1, ab ( YOB ) cluster (ROB)
outreg2 using "$dir3\first_stage_early.tex", keep(shock_earlyi) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)


areg  shock_early_life1 shock_earlyi1 gender genderhd i.agey i.year i.ROB#c.YOB i.ROB [aw=weight] if smpl_2==1, ab ( YOB ) cluster (ROB)
outreg2 using "$dir3\first_stage_early.tex", keep(shock_earlyi) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
 
/////
areg shock_1 shock_1i gender genderhd i.agey i.year i.ROB [aw=weight] if smpl_3==1, ab ( YOB ) cluster (ROB)
outreg2 using "$dir3\first_stage_inutero.tex", keep(shock_1i) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
 
areg shock_1 shock_1i gender genderhd i.agey i.year  i.ROB#c.YOB i.ROB [aw=weight] if smpl_4==1, ab ( YOB ) cluster (ROB)
outreg2 using "$dir3\first_stage_inutero.tex", keep(shock_1i) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

////Table D.4: Rainfall control
///Panel A
areg years_school shock_early_life1 avg_rain_early gender genderhd i.agey i.year i.ROB#c.YOB i.ROB [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  

outreg2 using "$dir3\rain_cognitive_early.tex", keep(shock_early_life1 avg_rain_early ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
///////////////////////////////////
///panel B
areg years_school shock_1 avg_rain_1 gender genderhd i.agey i.year i.ROB#c.YOB i.ROB [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  

outreg2 using "$dir3\rain_cognitive_inutero.tex", keep(shock_1 avg_rain_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

////////////////////////////////////
///Table D.7: houshod fixed effect

xtset hid
keep if sibsample==1
xi, pref(rid) i.ROB*YOB
///panel A
xi: xtreg years_school shock_early_life1 gender  i.YOB rid* i.year, fe r cluster (hid)
boottest shock_early_life1=0 ,  nograph  

outreg2 using "$dir3\household_cognitive_earely.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes, Interview Year FE, Yes, Household FE, Yes)
////////////////////////////////////////////////////////////////////////////
//panel B
xi: xtreg years_school shock_1 gender i.YOB rid* i.year, fe r cluster (hid)
boottest shock_1=0 ,  nograph  

outreg2 using "$dir3\household_cognitive_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes, Interview Year FE, Yes, Household FE, Yes)

///Table D.9: using intensity
///panel A
use "$dir1\Grade_final_early_life", clear

areg years_school shock_intens_early1 gender genderhd i.agey i.ROB#c.YOB i.ROB i.year [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_intens_early1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\shock_intens_early.tex", keep(shock_intens_early1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////////////////////////////////////////////////////
///panel B
areg years_school shock_intens gender genderhd i.agey i.ROB#c.YOB i.ROB i.year [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_intens=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\shock_intens_inutero.tex", keep(shock_intens) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

////////////////////////////////////////////////////////
////Table D.11: droping acra and volta

drop if ROB==4
drop if ROB==3
///panel A
areg years_school shock_early_life1 gender genderhd i.ROB i.ROB#c.YOB i.agey i.year [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\noacranovolta_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
////////////////////////////////////////////////////////////
///panel B
areg years_school shock_1 gender genderhd i.ROB i.ROB#c.YOB i.agey i.year [aw=weight], ab (YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\noacranovolta_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

/////////////////////////////////////
///Table D.13: high vs low price

///panel A
gen shockboom=High_early*Cocoa_producerb
gen shockrecesion=Low_early*Cocoa_producerb
****************************************
gen shocksecond=second*Cocoa_producerb
gen shockthird=third*Cocoa_producerb

gen shockfourth=fourth*Cocoa_producerb


******************************************************
areg years_school shockboom shockrecesion gender genderhd i.agey i.year  i.ROB#c.YOB i.ROB [aw=weight], ab ( YOB ) cluster (ROB)
boottest shockboom=0 ,  nograph 

boottest shockrecesion=0 ,  nograph  
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shockboom shockrecesion ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
********************************8
areg years_school  shocksecond  shockthird shockfourth gender genderhd i.agey i.year  i.ROB#c.YOB i.ROB [aw=weight], ab (YOB ) cluster (ROB)
boottest shocksecond=0 ,  nograph  

boottest shockthird=0 ,  nograph  

boottest shockfourth=0 ,  nograph  

outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shocksecond  shockthird shockfourth) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

////panel B

gen shockboom1=High_utero*Cocoa_producerb
gen shockrecesion1=Low_utero*Cocoa_producerb
*********************************
gen shocksecond_1=second_1*Cocoa_producerb

gen shockthird_1=third_1*Cocoa_producerb

gen shockfourth_1=fourth_1*Cocoa_producerb

****************************************
areg years_school shockboom1 shockrecesion1 gender genderhd  i.agey i.year i.ROB#c.YOB i.ROB [aw=weight], ab (YOB ) cluster (ROB)
boottest shockboom1=0 ,  nograph  

boottest shockrecesion1=0 ,  nograph  
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shockboom1 shockrecesion1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

areg years_school  shocksecond_1  shockthird_1 shockfourth_1  gender genderhd  i.agey i.year i.ROB#c.YOB i.ROB [aw=weight], ab (YOB ) cluster (ROB)
 boottest shocksecond_1=0 ,  nograph  

boottest shockthird_1=0 ,  nograph  

boottest shockfourth_1=0 ,  nograph  
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shocksecond_1  shockthird_1 shockfourth_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
////////////////////////////////////////////////////////////////////////////////////////////

//////Table D.16: Hetrogeniety by gender

///panel A

areg years_school shock_early_life1 gender genderhd i.ROB#c.YOB i.ROB i.agey i.year [aw=weight] if gender==1, ab (   YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph 

outreg2 using "$dir3\cocoa_cognitive_gender_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

use "$dir2\schooling_glss_1987_2012_earlylife_full", clear

areg years_school shock_early_life1  gender genderhd i.ROB#c.YOB i.ROB i.agey i.year  [aw=weight] if gender==0, ab (   YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph 

outreg2 using "$dir3\cocoa_cognitive_gender_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////////////////////////////////
///panel B
use "$dir2\schooling_glss_1987_2012_earlylife_full", clear

areg years_school shock_1 gender genderhd i.ROB#c.YOB i.ROB i.agey i.year  [aw=weight] if gender==1, ab (   YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph 

outreg2 using "$dir3\cocoa_cognitive_gender_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

use "$dir2\schooling_glss_1987_2012_earlylife_full", clear

areg years_school shock_1  gender genderhd i.ROB#c.YOB i.ROB i.agey i.year  [aw=weight] if gender==0, ab (   YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph 

outreg2 using "$dir3\cocoa_cognitive_gender_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
/////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
*******************************************************************************
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
